<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拷贝</title>
    <script>
        //1.浅拷贝
        let obj={
            name:"张三",
            age:18,
        };
        // let obj1=obj;
        // obj1.name="李四"
        // console.log(obj);
        let obj1={};
        for (let i in obj) {
            obj1[i]=obj[i];
        }
        obj1.name="李四"
        console.log(obj);
        // ------------------------
        let newObj = {
            name:"张三",
            age:18,
            family:{
                father:"张爸",
                mother:"张妈"
            }
        };
        let newobj1={};
        for (let i in newObj) {
            if(typeof newObj[i]==="object"){
                for (let j in newObj[i]){
                    newobj1[i]={};
                    newobj1[i][j]=newObj[i][j];
                }
            }else{
                newobj1[i]=newObj[i];
            }  
        }
        newobj1.family.father="李四"
        console.log(newObj);
        //------------------
        let Obj1 = {
            name:"张三",
            age:18,
            family:{
                father:{
                    name:"张爸",
                    age:45
                },
                mother:{
                    name:"张妈",
                    age:46
                },
            }
        };
        function deepCopy(obj){
            let newobj={};
            for(let i in obj){
                if(typeof obj[i]==="object"){
                    newobj[i]={};
                    newobj[i]=deepCopy(obj[i]);
                }else{
                    newobj[i]=obj[i];
                }
            }
            return newobj;
        }
        let newObj1=deepCopy(Obj1);
        newObj1.family.father.name="李四"
        console.log(Obj1);
    </script>
</head>
<body>
    
</body>
</html>